package com.test.mooan.labama.activity_fragment_boom.ln_boom;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.bean_boom.user_info_boom.RpInfo_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.view_boom.widget_boom.TitleView_boom;

import java.util.List;
import java.util.Map;

public class RepaymentActivity extends BaseLnActivity_boom {

    private TextView currencyTv;
    private TextView totalRepaymentTv;
    private TextView dueDateTv;
    private LinearLayout detailsViewGroup;

    private Product product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repayment);

        pageTag = "REPAY";

        try {
            product = (Product) getIntent().getSerializableExtra("product");
            if (product==null) product = Product.gtInstance(null);
        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }

        SwipeRefreshLayout refreshLayout = findViewById(R.id.activity_rp_refresh_view);
        TitleView_boom titleView = findViewById(R.id.activity_rp_title_view);
        setBackBtn_boom(titleView.getBackIb());

        titleView.setTitle(product.gtName());

        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpManager_boom.getInstance().getRepayDetail(product.gtProduct(),product.gtToken());
        });

        currencyTv = findViewById(R.id.activity_rp_tv_currency);
        totalRepaymentTv = findViewById(R.id.activity_rp_tv_total_repayment);
        dueDateTv = findViewById(R.id.activity_rp_tv_due_date);
        detailsViewGroup = findViewById(R.id.activity_rp_layout_details_group);

        findViewById(R.id.activity_rp_btn_submit).setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_SUBMIT_CLICK").toUpperCase(),product.gtProduct());
            HttpManager_boom.getInstance().saveUserPointForBatch();
            if (CountryInfo_boom.gInstance().isOrderRepayH5()) HttpManager_boom.getInstance().orderRepayH5_gh_cl_vi(product.gtProduct(),product.gtToken());
            else if (CountryInfo_boom.gInstance().isGetOrderRepayH5()) HttpManager_boom.getInstance().orderRepayH5_ug_ke(product.gtProduct(),product.gtToken());

        });

        findViewById(R.id.activity_rp_btn_repay).setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_REPAY_CLICK").toUpperCase(),product.gtProduct());
            HttpManager_boom.getInstance().saveUserPointForBatch();
            if (CountryInfo_boom.gInstance().isOrderRepayH5()) HttpManager_boom.getInstance().orderRepayH5_gh_cl_vi(product.gtProduct(),product.gtToken());
            else if (CountryInfo_boom.gInstance().isGetOrderRepayH5()) HttpManager_boom.getInstance().orderRepayH5_ug_ke(product.gtProduct(),product.gtToken());
        });

        onRepayDetailResponse();

    }

    @Override
    public void onRepayDetailResponse() {
        if (isClose()) return;

        totalRepaymentTv.setText(RpInfo_boom.getInstance().gRepayAmount());
        currencyTv.setText(RpInfo_boom.getInstance().gCurrency());
        dueDateTv.setText(RpInfo_boom.getInstance().gRepayDate());

        List<Map<String,String>> detailsList = RpInfo_boom.getInstance().gDetailItemList();

        detailsViewGroup.removeAllViews();

        View detailsView = null;
        LinearLayout detailsGroup = null;

        for (Map<String,String> details : detailsList){

            String title = Stringutil_boom.getSafeString(details.get("title"));
            String showName = Stringutil_boom.getSafeString(details.get("showName"));
            String showDetail = Stringutil_boom.getSafeString(details.get("showDetail"));
            String oldDetail = Stringutil_boom.getSafeString(details.get("oldDetail"));
            String discount = Stringutil_boom.getSafeString(details.get("discount"));

            if (detailsView==null || "YES".equalsIgnoreCase(title) || "true".equalsIgnoreCase(title)){

                if (detailsView!=null) detailsViewGroup.addView(detailsView);
                detailsView = View.inflate(this,R.layout.view_ln_details,null);
                TextView titleNameTv = detailsView.findViewById(R.id.view_ln_details_tv_title_name);
                TextView titleValueTv = detailsView.findViewById(R.id.view_ln_details_tv_title_value);
                detailsGroup = detailsView.findViewById(R.id.view_ln_details_layout_group);
                titleNameTv.setText(showName);
                titleValueTv.setText(showDetail);

            }else {

                View detailItem = View.inflate(this,R.layout.item_ln_detail,null);

                TextView nameTv = detailItem.findViewById(R.id.item_ln_detail_tv_name);
                View oldValueView = detailItem.findViewById(R.id.item_ln_detail_view_old_value);
                TextView oldValueTv = detailItem.findViewById(R.id.item_ln_detail_tv_old_value);
                TextView valueTv = detailItem.findViewById(R.id.item_ln_detail_tv_value);
                View discountView = detailItem.findViewById(R.id.item_ln_detail_view_discount);
                TextView discountTv = detailItem.findViewById(R.id.item_ln_detail_tv_discount);

                nameTv.setText(showName);
                valueTv.setText(showDetail);

                if (TextUtils.isEmpty(oldDetail) || TextUtils.isEmpty(discount)){
                    oldValueView.setVisibility(View.GONE);
                    discountView.setVisibility(View.GONE);
                }else {
                    oldValueView.setVisibility(View.VISIBLE);
                    discountView.setVisibility(View.VISIBLE);
                    oldValueTv.setText(oldDetail);
                    discountTv.setText(discount);
                }

                detailsGroup.addView(detailItem);

            }

        }

        if (detailsView!=null) detailsViewGroup.addView(detailsView);
    }

    public void onOrderRepayH5Response(String url) {
        if (isClose() || TextUtils.isEmpty(url)) return;
        Uri uri = Uri.parse(Stringutil_boom.getSafeString(url));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

}